﻿using System;
using System.Text;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Net;
using System.Xml;
using System.Xml.Serialization;
using Sukul.SharePoint.Taxomatic;
using DTO = Sukul.SharePoint.Taxomatic.DTO;
using Sukul.SharePoint.Taxomatic.Service;
using System.IO;

namespace Taxomatic.UnitTests
{
    [TestClass]
    public class TaxonomyTest
    {
        const string WEBSITE_URL = @"http://learningsukul.org:8082";
        const string SERVICE_URL = WEBSITE_URL + "/_vti_bin/Sukul.SharePoint.Taxomatic/TaxomaticService.svc";
        const string METHOD1_URL = SERVICE_URL + "/GetTerms";
        const string METHOD_TEST_URL = SERVICE_URL + "/Test";
        ICredentials _TestUser;

        public TaxonomyTest()
        {
            /* Set the test user here */
            _TestUser = CredentialCache.DefaultNetworkCredentials;
            Helper.Instance.ActiveUser = _TestUser;
        }

        public object DeSerialize(string xml, Type t)
        {
            XmlSerializer ser = new XmlSerializer(t);
            StringReader input = new StringReader(xml);
            return ser.Deserialize(input);
        }

       // [TestMethod]
        public void XmlDeserialize()
        {

            string xml = System.IO.File.ReadAllText(@"D:\TEMP\Taxomatic.Export3.xml");
            DTO.TermStores stores = new DTO.TermStores();
            stores = DeSerialize(xml, stores.GetType()) as DTO.TermStores;

        }

        //[TestMethod]
        //public void LoadTermStoreTest()
        //{
        //    try
        //    {
        //        DTO.WebServiceResponse request = new DTO.WebServiceResponse();
        //        string data = "{\"request\": " + JsonConvert.SerializeObject(request) + "}";
        //        string s_response = Helper.Instance.CleanJSONObject(Helper.Instance.HttpResponse(METHOD1_URL, data, "POST"));

        //        /* Strong typed response object */
        //        var responseTypedObj = JsonConvert.DeserializeObject<DTO.WebServiceResponse>(s_response);

        //        /* This test simulates an error condition */
        //        Assert.IsTrue(responseTypedObj.IsSuccess);

        //        /* Retrieve the message body */
        //        JArray termStores = JArray.Parse(responseTypedObj.Payload.ToString());

        //        foreach (var store in termStores)
        //        {
        //            Console.WriteLine(string.Format("Term Store Name: {0}", store["Name"].ToString()));

        //            foreach (var group in (JArray)store["Groups"])
        //            {
        //                Console.WriteLine(string.Format("Group Name: {0}{1}", "\t", group["Name"].ToString()));
        //                foreach (var termset in (JArray)group["Termsets"])
        //                {
        //                    Console.WriteLine("Termset Name: {0}{1}", "\t\t", termset["Name"].ToString());
        //                    if (null != termset["Terms"])
        //                    {
        //                        OutputTerms((JArray)termset["Terms"], 3);
        //                    }
        //                }
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Console.WriteLine(ex.ToString());
        //        Assert.Fail(ex.Message);
        //    }
        //}

        //private void OutputTerms(JArray terms, int indent)
        //{
        //    if (null != terms && terms.Count > 0)
        //    {
        //        foreach (var term in terms)
        //        {
        //            Console.WriteLine("Term Name: {0}{1}", new String('\t', indent), term["Name"].ToString());
        //            if (null != term["Terms"] && ((JArray)term["Terms"]).Count > 0)
        //            {
        //                OutputTerms((JArray)term["Terms"], (indent + 1));
        //            }
        //        }
        //    }
        //}

    }
}
